<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <el-card shadow="never">
          <el-alert
            title="三级路由【缓存路由、固定表格高度、并根据窗口大小自适应】的示例。详情页请点击操作列按钮，支持tab多开并高亮左侧菜单"
            type="success"
            :closable="false"
            show-icon
          >
          </el-alert>
          <el-col :span="24" class="table-from">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
              <el-form-item label="标题">
                <el-input v-model="formInline.title"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmitTitle" icon="el-icon-search">查询</el-button>
              </el-form-item>
              <el-form-item label="作者">
                <el-input v-model="formInline.name"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="onSubmitName" icon="el-icon-search">查询</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="24" class="form">
            <el-button type="primary" size="medium" icon="el-icon-plus"
                       @click="dialogVisible = true;dialogTitle = '添加';resetForm('ruleForm')"
            >添加
            </el-button>
            <el-button type="danger" size="medium" icon="el-icon-delete" @click="deleteItem()">删除</el-button>
            <el-button type="primary" size="medium" @click="messageItem()">消息提示</el-button>
            <el-button type="primary" size="medium" @click="messageBoxItem()">弹框</el-button>
            <el-button type="primary" size="medium" @click="NotificationItem()">通知</el-button>
            <el-button type="primary" size="medium">对话框</el-button>
            <el-button type="primary" size="medium" @click="detailItem()">详情页</el-button>

            <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="标题" prop="title">
                  <el-input type="text" v-model="ruleForm.title"></el-input>
                </el-form-item>
                <el-form-item label="作者" prop="name">
                  <el-input type="text" v-model="ruleForm.name"></el-input>
                </el-form-item>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
              </span>
            </el-dialog>
          </el-col>

          <el-col :span="24" class="table">

            <el-table ref="recordTable"
                      @selection-change="handleSelectionChange"
                      :data="tableData.slice((pages.pageNum-1)*pages.pageSize,pages.pageNum*pages.pageSize)"
                      height="360" :loading="loading" :row-key="getRowKey"
            >
              <el-table-column type="selection" width="65px" :reserve-selection="true"></el-table-column>
              <el-table-column label="序号" type="index"></el-table-column>

              <el-table-column label="标题" prop="title"></el-table-column>
              <el-table-column label="作者" prop="name"></el-table-column>
              <el-table-column label="评级" prop="rate" dateFormat="datetime">
                <template slot-scope="scope">
                  <el-rate
                    :max="3"
                    :value="scope.row.rate"
                    :colors="colors"
                  >
                  </el-rate>
                </template>
              </el-table-column>
              <el-table-column label="头像" prop="address" dateFormat="datetime">
                <template slot-scope="scope">
                  <el-avatar :src="scope.row.avatar"></el-avatar>
                </template>

              </el-table-column>
              <el-table-column label="点击量" sortable prop="clickNum">

              </el-table-column>
              <el-table-column label="开关" prop="switch" dateFormat="datetime">
                <template slot-scope="scope">
                  <el-switch
                    @click.native="clickSwitch(scope.row)"
                    :value="scope.row.switch"
                  >
                  </el-switch>
                </template>

              </el-table-column>
              <el-table-column label="状态" prop="state" dateFormat="datetime">
                <template slot-scope="scope">
                  <el-tag v-if="scope.row.state==='draft'">{{ scope.row.state }}</el-tag>
                  <el-tag type="success" v-else-if="scope.row.state==='published'">{{ scope.row.state }}</el-tag>
                  <el-tag type="danger" v-else>{{ scope.row.state }}</el-tag>
                </template>

              </el-table-column>
              <el-table-column label="时间" prop="date" dateFormat="datetime" width="150">
              </el-table-column>
              <el-table-column label="操作" width="150">
                <template slot-scope="scope">
                  <div>
                    <el-button size="mini" type="text" @click.native.stop="handleDetail(scope.row)">详情</el-button>
                    <el-button
                      size="mini"
                      @click="handleEdit(scope.row)"
                    >编辑
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <Pagination :total="total" :pageNum.sync="pages.pageNum"
                        :limit.sync="pages.pageSize"
                        :pageSizes="pageSizes"
            ></Pagination>
          </el-col>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'
import Pagination from '@/components/Pagination/Pagination'

export default {
  name: 'synthesize',
  components: { Pagination },
  data() {
    return {
      multipleSelection: [],
      dialogTitle: '',
      ruleForm: {
        name: '',
        title: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入作者', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      pageSizes: [10, 20, 30, 40, 50, 100],
      colors: ['#99A9BF', '#F7BA2A', '#FF9900'],
      pages: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      tableData: [], //表格数据
      dataList: [],
      loading: false, //是否在加载数据
      formInline: {
        title: '',
        name: ''
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    resetForm(formName) {
      this.$nextTick(() => {
        this.$refs[formName].resetFields()
      })
    },
    messageItem() {
      this.$message({
        message: '河北彩花',
        type: 'success'
      })
    },
    messageBoxItem() {
      this.$alert('这是一段内容', '标题名称', {
        confirmButtonText: '确定'
      })
    },
    NotificationItem() {
      const h = this.$createElement
      this.$notify({
        title: '标题名称',
        message: h('i', { style: 'color: teal' }, '这是提示文案这是提示文案这是提示文案')
      })
    },
    detailItem() {
      if (this.multipleSelection.length <= 0 || this.multipleSelection.length > 1) {
        this.$message.error('请选择一行进行详情页跳转')
      } else {
        this.$router.push({ name: 'tableDetail', query: { id: this.multipleSelection[0].id } })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteItem() {
      if (this.multipleSelection.length <= 0) {
        this.$message.error('未选中任何行')
      } else {
        this.$message({
          message: '模拟删除成功',
          type: 'success'
        })
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          this.$message({
            message: '模拟保存成功',
            type: 'success'
          })
        } else {
          return false
        }
      })
    },
    clickSwitch(row) {
      row.switch = !row.switch
    },
    getRowKey(row) {
      return row.id
    },
    fetchData() {
      //请求接口加载表格数据
      this.loading = true
      axios.get('http://route.showapi.com/60-27').then(res => {
        this.tableData = res.data._data.data.rows
        this.dataList = res.data._data.data.rows
        this.$store.commit('user/saveData', res.data._data.data.rows)
        this.total = res.data._data.data.total
        this.loading = false
      }).catch(err => {
        this.loading = false
      })
    },

    handleDetail(row) {
      this.$router.push({ name: 'tableDetail', query: { id: row.id } })
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.resetForm('ruleForm')
      this.$nextTick(() => {
        this.dialogTitle = '编辑'
        this.ruleForm.title = row.title
        this.ruleForm.name = row.name
      })
    },

    onSubmitTitle() {
      let list = []
      this.tableData = this.dataList
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].title.indexOf(this.formInline.title) >= 0) {
          list.push(this.tableData[i])
        }
      }
      this.tableData = list
      this.total = this.tableData.length
    },
    onSubmitName() {
      let list = []
      this.tableData = this.dataList
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].name.indexOf(this.formInline.name) >= 0) {
          list.push(this.tableData[i])
        }
      }
      this.tableData = list
      this.total = this.tableData.length
    }
  }
}
</script>

<style scoped lang="scss">
.box {
  ::v-deep.el-alert__icon {
    margin-top: 3px;
  }

  ::v-deep.el-icon-search {
    font-size: 14px;
  }

  ::v-deep.el-form-item__label {
    font-weight: 400;
  }

  .table-from {
    padding: 20px 10px;
  }

  .form {
    ::v-deep.el-input {
      width: 380px;
    }

    ::v-deep.el-dialog {
      width: 580px;
    }
  }

  .table {
    padding: 20px 0px;

    ::v-deep.cell {
      font-size: 14px;
      font-weight: 400;
      text-align: center;
    }

    ::v-deep.el-tag {
      width: 70px;
      display: inline-block;
    }
  }
}
</style>
